{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04',\n",
       "               '2013-01-05', '2013-01-06'],\n",
       "              dtype='datetime64[ns]', freq='D')"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd \n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "dates=pd.date_range('20130101',periods=6)\n",
    "dates\n",
    "\n",
    "#date_range是pandas的一个方法。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "?pd.date_range()\n",
    "#可以查看该方法的用法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2013-01-01</th>\n",
       "      <td>-0.170378</td>\n",
       "      <td>0.603429</td>\n",
       "      <td>0.271953</td>\n",
       "      <td>-0.307458</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-02</th>\n",
       "      <td>-0.333254</td>\n",
       "      <td>-1.174691</td>\n",
       "      <td>-0.083088</td>\n",
       "      <td>2.245154</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-03</th>\n",
       "      <td>1.734610</td>\n",
       "      <td>-1.880908</td>\n",
       "      <td>-1.411285</td>\n",
       "      <td>0.699110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-04</th>\n",
       "      <td>0.417770</td>\n",
       "      <td>1.439819</td>\n",
       "      <td>-0.374155</td>\n",
       "      <td>-1.644983</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-05</th>\n",
       "      <td>-0.271914</td>\n",
       "      <td>-0.337900</td>\n",
       "      <td>-0.674226</td>\n",
       "      <td>1.699197</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-06</th>\n",
       "      <td>1.001164</td>\n",
       "      <td>-1.285517</td>\n",
       "      <td>-0.409395</td>\n",
       "      <td>1.796215</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2013-01-01 -0.170378  0.603429  0.271953 -0.307458\n",
       "2013-01-02 -0.333254 -1.174691 -0.083088  2.245154\n",
       "2013-01-03  1.734610 -1.880908 -1.411285  0.699110\n",
       "2013-01-04  0.417770  1.439819 -0.374155 -1.644983\n",
       "2013-01-05 -0.271914 -0.337900 -0.674226  1.699197\n",
       "2013-01-06  1.001164 -1.285517 -0.409395  1.796215"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df=pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))\n",
    "df\n",
    "#np.random.randn(6,4)用来生成6行4列的标准正太分布数列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    float64\n",
       "B    float64\n",
       "C    float64\n",
       "D    float64\n",
       "dtype: object"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.dtypes\n",
    "#查看数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df.head()\n",
    "df.tail()\n",
    "#查看数据前几个和后几个\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['A', 'B', 'C', 'D'], dtype='object')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.index\n",
    "#显示索引\n",
    "df.columns\n",
    "#显示列名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>6.000000</td>\n",
       "      <td>6.000000</td>\n",
       "      <td>6.000000</td>\n",
       "      <td>6.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>0.396333</td>\n",
       "      <td>-0.439295</td>\n",
       "      <td>-0.446699</td>\n",
       "      <td>0.747873</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.831538</td>\n",
       "      <td>1.262044</td>\n",
       "      <td>0.572280</td>\n",
       "      <td>1.489144</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>-0.333254</td>\n",
       "      <td>-1.880908</td>\n",
       "      <td>-1.411285</td>\n",
       "      <td>-1.644983</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>-0.246530</td>\n",
       "      <td>-1.257811</td>\n",
       "      <td>-0.608018</td>\n",
       "      <td>-0.055816</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>0.123696</td>\n",
       "      <td>-0.756295</td>\n",
       "      <td>-0.391775</td>\n",
       "      <td>1.199153</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>0.855316</td>\n",
       "      <td>0.368097</td>\n",
       "      <td>-0.155855</td>\n",
       "      <td>1.771960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>1.734610</td>\n",
       "      <td>1.439819</td>\n",
       "      <td>0.271953</td>\n",
       "      <td>2.245154</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              A         B         C         D\n",
       "count  6.000000  6.000000  6.000000  6.000000\n",
       "mean   0.396333 -0.439295 -0.446699  0.747873\n",
       "std    0.831538  1.262044  0.572280  1.489144\n",
       "min   -0.333254 -1.880908 -1.411285 -1.644983\n",
       "25%   -0.246530 -1.257811 -0.608018 -0.055816\n",
       "50%    0.123696 -0.756295 -0.391775  1.199153\n",
       "75%    0.855316  0.368097 -0.155855  1.771960\n",
       "max    1.734610  1.439819  0.271953  2.245154"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.describe()\n",
    "#描述性统计"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>2013-01-01 00:00:00</th>\n",
       "      <th>2013-01-02 00:00:00</th>\n",
       "      <th>2013-01-03 00:00:00</th>\n",
       "      <th>2013-01-04 00:00:00</th>\n",
       "      <th>2013-01-05 00:00:00</th>\n",
       "      <th>2013-01-06 00:00:00</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>-0.170378</td>\n",
       "      <td>-0.333254</td>\n",
       "      <td>1.734610</td>\n",
       "      <td>0.417770</td>\n",
       "      <td>-0.271914</td>\n",
       "      <td>1.001164</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>0.603429</td>\n",
       "      <td>-1.174691</td>\n",
       "      <td>-1.880908</td>\n",
       "      <td>1.439819</td>\n",
       "      <td>-0.337900</td>\n",
       "      <td>-1.285517</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>0.271953</td>\n",
       "      <td>-0.083088</td>\n",
       "      <td>-1.411285</td>\n",
       "      <td>-0.374155</td>\n",
       "      <td>-0.674226</td>\n",
       "      <td>-0.409395</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>-0.307458</td>\n",
       "      <td>2.245154</td>\n",
       "      <td>0.699110</td>\n",
       "      <td>-1.644983</td>\n",
       "      <td>1.699197</td>\n",
       "      <td>1.796215</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   2013-01-01  2013-01-02  2013-01-03  2013-01-04  2013-01-05  2013-01-06\n",
       "A   -0.170378   -0.333254    1.734610    0.417770   -0.271914    1.001164\n",
       "B    0.603429   -1.174691   -1.880908    1.439819   -0.337900   -1.285517\n",
       "C    0.271953   -0.083088   -1.411285   -0.374155   -0.674226   -0.409395\n",
       "D   -0.307458    2.245154    0.699110   -1.644983    1.699197    1.796215"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.T\n",
    "#转置调换数据，相当于矩阵转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2013-01-01</th>\n",
       "      <td>-0.170378</td>\n",
       "      <td>0.603429</td>\n",
       "      <td>0.271953</td>\n",
       "      <td>-0.307458</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-02</th>\n",
       "      <td>-0.333254</td>\n",
       "      <td>-1.174691</td>\n",
       "      <td>-0.083088</td>\n",
       "      <td>2.245154</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-03</th>\n",
       "      <td>1.734610</td>\n",
       "      <td>-1.880908</td>\n",
       "      <td>-1.411285</td>\n",
       "      <td>0.699110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-04</th>\n",
       "      <td>0.417770</td>\n",
       "      <td>1.439819</td>\n",
       "      <td>-0.374155</td>\n",
       "      <td>-1.644983</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-05</th>\n",
       "      <td>-0.271914</td>\n",
       "      <td>-0.337900</td>\n",
       "      <td>-0.674226</td>\n",
       "      <td>1.699197</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-06</th>\n",
       "      <td>1.001164</td>\n",
       "      <td>-1.285517</td>\n",
       "      <td>-0.409395</td>\n",
       "      <td>1.796215</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2013-01-01 -0.170378  0.603429  0.271953 -0.307458\n",
       "2013-01-02 -0.333254 -1.174691 -0.083088  2.245154\n",
       "2013-01-03  1.734610 -1.880908 -1.411285  0.699110\n",
       "2013-01-04  0.417770  1.439819 -0.374155 -1.644983\n",
       "2013-01-05 -0.271914 -0.337900 -0.674226  1.699197\n",
       "2013-01-06  1.001164 -1.285517 -0.409395  1.796215"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sort_index(axis=0,ascending=True)\n",
    "#按轴排序\n",
    "#?df.sort_index(axis=1,ascending=False)\n",
    "#查看方法\n",
    "#axis=0对应的是对左边一列的index进行排序，ascending=False代表降序，ascending=True代表升序\n",
    "#axis=1对应的是对上边一行的index进行排序，同样的，ascending=False代表降序，ascending=True代表升序\n",
    "#只对行标签和列标签进行排序。\n",
    "#0表示index，即一行，1表示columns即一列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "E:\\anaconda\\lib\\site-packages\\ipykernel_launcher.py:1: FutureWarning: by argument to sort_index is deprecated, pls use .sort_values(by=...)\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2013-01-03</th>\n",
       "      <td>1.734610</td>\n",
       "      <td>-1.880908</td>\n",
       "      <td>-1.411285</td>\n",
       "      <td>0.699110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-06</th>\n",
       "      <td>1.001164</td>\n",
       "      <td>-1.285517</td>\n",
       "      <td>-0.409395</td>\n",
       "      <td>1.796215</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-02</th>\n",
       "      <td>-0.333254</td>\n",
       "      <td>-1.174691</td>\n",
       "      <td>-0.083088</td>\n",
       "      <td>2.245154</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-05</th>\n",
       "      <td>-0.271914</td>\n",
       "      <td>-0.337900</td>\n",
       "      <td>-0.674226</td>\n",
       "      <td>1.699197</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01</th>\n",
       "      <td>-0.170378</td>\n",
       "      <td>0.603429</td>\n",
       "      <td>0.271953</td>\n",
       "      <td>-0.307458</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-04</th>\n",
       "      <td>0.417770</td>\n",
       "      <td>1.439819</td>\n",
       "      <td>-0.374155</td>\n",
       "      <td>-1.644983</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2013-01-03  1.734610 -1.880908 -1.411285  0.699110\n",
       "2013-01-06  1.001164 -1.285517 -0.409395  1.796215\n",
       "2013-01-02 -0.333254 -1.174691 -0.083088  2.245154\n",
       "2013-01-05 -0.271914 -0.337900 -0.674226  1.699197\n",
       "2013-01-01 -0.170378  0.603429  0.271953 -0.307458\n",
       "2013-01-04  0.417770  1.439819 -0.374155 -1.644983"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sort_index(by='B')\n",
    "#仅对B列数据进行排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df.A\n",
    "df.['A']\n",
    "#取A列数据，返回一个序列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2013-01-02</th>\n",
       "      <td>-0.333254</td>\n",
       "      <td>-1.174691</td>\n",
       "      <td>-0.083088</td>\n",
       "      <td>2.245154</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-03</th>\n",
       "      <td>1.734610</td>\n",
       "      <td>-1.880908</td>\n",
       "      <td>-1.411285</td>\n",
       "      <td>0.699110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-04</th>\n",
       "      <td>0.417770</td>\n",
       "      <td>1.439819</td>\n",
       "      <td>-0.374155</td>\n",
       "      <td>-1.644983</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2013-01-02 -0.333254 -1.174691 -0.083088  2.245154\n",
       "2013-01-03  1.734610 -1.880908 -1.411285  0.699110\n",
       "2013-01-04  0.417770  1.439819 -0.374155 -1.644983"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[0:3]\n",
    "#切片，使用默认索引，返回前三行\n",
    "df['20130102':'20130104']\n",
    "#使用新索引取对应行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2013-01-02</th>\n",
       "      <td>-0.333254</td>\n",
       "      <td>-1.174691</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-03</th>\n",
       "      <td>1.734610</td>\n",
       "      <td>-1.880908</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-04</th>\n",
       "      <td>0.417770</td>\n",
       "      <td>1.439819</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B\n",
       "2013-01-02 -0.333254 -1.174691\n",
       "2013-01-03  1.734610 -1.880908\n",
       "2013-01-04  0.417770  1.439819"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[dates[0]]\n",
    "#取出第一行\n",
    "df.loc[:,['A','B']]\n",
    "#通过标签选择多列\n",
    "df.loc['20130102':'20130104',['A','B']]\n",
    "#按照行列标签切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#按照位置选择\n",
    "df.iloc[3]\n",
    "#取出第三行\n",
    "df.iloc[3:5,0:2]\n",
    "#取3-4行，1-2列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2013-01-03</th>\n",
       "      <td>1.734610</td>\n",
       "      <td>-1.880908</td>\n",
       "      <td>-1.411285</td>\n",
       "      <td>0.699110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-04</th>\n",
       "      <td>0.417770</td>\n",
       "      <td>1.439819</td>\n",
       "      <td>-0.374155</td>\n",
       "      <td>-1.644983</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-06</th>\n",
       "      <td>1.001164</td>\n",
       "      <td>-1.285517</td>\n",
       "      <td>-0.409395</td>\n",
       "      <td>1.796215</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2013-01-03  1.734610 -1.880908 -1.411285  0.699110\n",
       "2013-01-04  0.417770  1.439819 -0.374155 -1.644983\n",
       "2013-01-06  1.001164 -1.285517 -0.409395  1.796215"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#布尔索引\n",
    "df[df.A>0]\n",
    "#先判断A列中大于0的数，再把这些行取出来"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2013-01-01</th>\n",
       "      <td>-0.170378</td>\n",
       "      <td>0.603429</td>\n",
       "      <td>0.271953</td>\n",
       "      <td>-0.307458</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-02</th>\n",
       "      <td>-0.333254</td>\n",
       "      <td>-1.174691</td>\n",
       "      <td>-0.083088</td>\n",
       "      <td>2.245154</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-03</th>\n",
       "      <td>1.734610</td>\n",
       "      <td>-1.880908</td>\n",
       "      <td>-1.411285</td>\n",
       "      <td>0.699110</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-04</th>\n",
       "      <td>0.417770</td>\n",
       "      <td>1.439819</td>\n",
       "      <td>-0.374155</td>\n",
       "      <td>-1.644983</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D    E\n",
       "2013-01-01 -0.170378  0.603429  0.271953 -0.307458  1.0\n",
       "2013-01-02 -0.333254 -1.174691 -0.083088  2.245154  1.0\n",
       "2013-01-03  1.734610 -1.880908 -1.411285  0.699110  NaN\n",
       "2013-01-04  0.417770  1.439819 -0.374155 -1.644983  NaN"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#缺失值处理，pandas用np.nan表示缺失的诗句，默认不包含在计算中。\n",
    "#重新索引允许更改/添加/删除指定轴上的索引。\n",
    "df1=df.reindex(index=dates[0:4],columns=list(df.columns)+['E'])\n",
    "df1.loc[dates[0]:dates[1],'E']=1\n",
    "#E列的第一个和第二个值赋值为1\n",
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2013-01-01</th>\n",
       "      <td>-0.170378</td>\n",
       "      <td>0.603429</td>\n",
       "      <td>0.271953</td>\n",
       "      <td>-0.307458</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-02</th>\n",
       "      <td>-0.333254</td>\n",
       "      <td>-1.174691</td>\n",
       "      <td>-0.083088</td>\n",
       "      <td>2.245154</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D    E\n",
       "2013-01-01 -0.170378  0.603429  0.271953 -0.307458  1.0\n",
       "2013-01-02 -0.333254 -1.174691 -0.083088  2.245154  1.0"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.dropna()\n",
    "#?df1.dropna()\n",
    "#df1.dropna(axis=0, how='any', thresh=None, subset=None, inplace=False)\n",
    "#参数axis=0表示行，axis=1表示列。 how='any'表示元素中包含空值，how='all'表示元素中全为空值。inplace表示十分替换\n",
    "df1.dropna(how='any')\n",
    "#默认axis=0表示删除行。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2013-01-01</th>\n",
       "      <td>-0.170378</td>\n",
       "      <td>0.603429</td>\n",
       "      <td>0.271953</td>\n",
       "      <td>-0.307458</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-02</th>\n",
       "      <td>-0.333254</td>\n",
       "      <td>-1.174691</td>\n",
       "      <td>-0.083088</td>\n",
       "      <td>2.245154</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-03</th>\n",
       "      <td>1.734610</td>\n",
       "      <td>-1.880908</td>\n",
       "      <td>-1.411285</td>\n",
       "      <td>0.699110</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-04</th>\n",
       "      <td>0.417770</td>\n",
       "      <td>1.439819</td>\n",
       "      <td>-0.374155</td>\n",
       "      <td>-1.644983</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D    E\n",
       "2013-01-01 -0.170378  0.603429  0.271953 -0.307458  1.0\n",
       "2013-01-02 -0.333254 -1.174691 -0.083088  2.245154  1.0\n",
       "2013-01-03  1.734610 -1.880908 -1.411285  0.699110  NaN\n",
       "2013-01-04  0.417770  1.439819 -0.374155 -1.644983  NaN"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.fillna(value=5)\n",
    "?df1.fillna(value=5)\n",
    "#这里用5填充，实际中常用0，中位数，平均值填充"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "#统计平均值\n",
    "df.mean()\n",
    "#每列的平均值\n",
    "df.mean(1)\n",
    "#每行的平均值\n",
    "?df.mean()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    2.067864\n",
       "B    3.320727\n",
       "C    1.683238\n",
       "D    3.890137\n",
       "dtype: float64"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#APPLY,将功能用于处理数据\n",
    "df.apply(np.cumsum)\n",
    "#列累加，就是第二列的结果等于第一列+第二列\n",
    "df.apply(lambda x:x.max()-x.min())\n",
    "#使用匿名函数求每列的最大值和最小值的差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.333254    1\n",
       " 1.001164    1\n",
       "-0.271914    1\n",
       " 1.734610    1\n",
       " 0.417770    1\n",
       "-0.170378    1\n",
       "Name: A, dtype: int64"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#对数据计数\n",
    "#只有序列有value_counts()方法\n",
    "df.A.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key</th>\n",
       "      <th>lval_x</th>\n",
       "      <th>lval_y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>apple</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>apple</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>apple</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>apple</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     key  lval_x  lval_y\n",
       "0  apple       1       4\n",
       "1  apple       1       5\n",
       "2  apple       2       4\n",
       "3  apple       2       5"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#join加入操作，合并。\n",
    "left =pd.DataFrame({'key':['apple','apple'],'lval':[1,2]})\n",
    "#字典转为df结构\n",
    "left\n",
    "right =pd.DataFrame({'key':['apple','apple'],'lval':[4,5]})\n",
    "right\n",
    "pd.merge(left,right,on='key')\n",
    "#链接之后的效果类似笛卡尔积\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key</th>\n",
       "      <th>lval_x</th>\n",
       "      <th>lval_y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>apple</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>orange</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      key  lval_x  lval_y\n",
       "0   apple       1       4\n",
       "1  orange       2       5"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "left1=pd.DataFrame({'key':['apple','orange'],'lval':[1,2]})\n",
    "right1=pd.DataFrame({'key':['apple','orange'],'lval':[4,5]})\n",
    "pd.merge(left1,right1,on='key')\n",
    "#按照key合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">apple</th>\n",
       "      <th>one</th>\n",
       "      <td>-0.926187</td>\n",
       "      <td>-0.149763</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>-3.449968</td>\n",
       "      <td>-1.337488</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">orange</th>\n",
       "      <th>one</th>\n",
       "      <td>0.180778</td>\n",
       "      <td>-1.703416</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>three</th>\n",
       "      <td>0.109862</td>\n",
       "      <td>1.487152</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     C         D\n",
       "A      B                        \n",
       "apple  one   -0.926187 -0.149763\n",
       "       two   -3.449968 -1.337488\n",
       "orange one    0.180778 -1.703416\n",
       "       three  0.109862  1.487152"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#分组group by\n",
    "df=pd.DataFrame({\n",
    "    'A':['apple','orange','apple','orange','apple','orange','apple','orange'],\n",
    "    'B':['one','one','two','three','one','one','two','three'],\n",
    "    'C':np.random.randn(8),\n",
    "    'D':np.random.randn(8)\n",
    "    })\n",
    "df\n",
    "df.groupby('A').sum()\n",
    "df.groupby('B').sum()\n",
    "df.groupby(['A','B']).sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#stack堆的数据结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>C</th>\n",
       "      <th>-2.9198578097</th>\n",
       "      <th>-0.950757557138</th>\n",
       "      <th>-0.544780566241</th>\n",
       "      <th>-0.530110105381</th>\n",
       "      <th>-0.359883748968</th>\n",
       "      <th>0.0245705327225</th>\n",
       "      <th>0.469746174962</th>\n",
       "      <th>0.725558479181</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">apple</th>\n",
       "      <th>one</th>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.367303</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.217539</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>-0.103147</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-1.234341</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">orange</th>\n",
       "      <th>one</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.251983</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-1.451433</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>three</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.801575</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.314423</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "C             -2.919858  -0.950758  -0.544781  -0.530110  -0.359884  \\\n",
       "A      B                                                              \n",
       "apple  one          NaN  -0.367303        NaN        NaN        NaN   \n",
       "       two    -0.103147        NaN        NaN  -1.234341        NaN   \n",
       "orange one          NaN        NaN  -0.251983        NaN        NaN   \n",
       "       three        NaN        NaN        NaN        NaN   1.801575   \n",
       "\n",
       "C              0.024571   0.469746   0.725558  \n",
       "A      B                                       \n",
       "apple  one     0.217539        NaN        NaN  \n",
       "       two          NaN        NaN        NaN  \n",
       "orange one          NaN        NaN  -1.451433  \n",
       "       three        NaN  -0.314423        NaN  "
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#数据透视表，df为数据名，values表示统计值，index表示索引列名，columns？\n",
    "pd.pivot_table(df,values='D',index=['A','B'],columns=['C'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
